package com.hp.modules.sys.service.impl;

import com.github.pagehelper.Page;
import com.hp.common.base.CommonPage;
import com.hp.common.base.WhereParam;
import com.hp.common.base.YesNoEnum;
import com.hp.common.tk.ConditionUtil;
import com.hp.mapper.SysDictItemMapper;
import com.hp.modules.sys.dto.SysDictItemPageParam;
import com.hp.modules.sys.entity.SysDictItem;
import com.hp.modules.sys.service.SysDictItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

import java.util.Date;
import java.util.List;

/**
 * 接口层
 * 字典项
 */
@Service
public class SysDictItemServiceImpl implements SysDictItemService {

    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    @Caching(evict = {@CacheEvict(value = "sys_dict_key")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(SysDictItem param) {
        Date now = new Date();
        SysDictItem sysDictItem = new SysDictItem();
        BeanUtils.copyProperties(param, sysDictItem);
        sysDictItem.setCreateTime(now);
        sysDictItem.setUpdateTime(now);
        sysDictItem.setIsDeleted(YesNoEnum.NO);
        return sysDictItemMapper.insertSelective(sysDictItem);
    }

    @Caching(evict = {@CacheEvict(value = "sys_dict_key")})
    @Override
    public int update(SysDictItem param) {
        Date now = new Date();
        SysDictItem sysDictItem = new SysDictItem();
        BeanUtils.copyProperties(param, sysDictItem);
        sysDictItem.setUpdateTime(now);
        return sysDictItemMapper.updateByPrimaryKeySelective(sysDictItem);
    }

    @Caching(evict = {@CacheEvict(value = "sys_dict_key")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int remove(SysDictItem param) {
        Date now = new Date();
        SysDictItem upUser = new SysDictItem();
        upUser.setUpdateTime(now);
        Condition condition = new Condition(SysDictItem.class);
        //更新时间
        sysDictItemMapper.updateByConditionSelective(upUser, condition);
        //逻辑删除
        return sysDictItemMapper.deleteByCondition(condition);
    }

    @Override
    public SysDictItem get(Long id) {
        return sysDictItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public CommonPage<SysDictItem> list(SysDictItemPageParam param) {
        Page<SysDictItem> page = param.buildPage(true);
        List<WhereParam> whereParams = param.getWhereParams();
        if (null == whereParams || whereParams.isEmpty()) {
            SysDictItem sysDictItem = new SysDictItem();
            sysDictItemMapper.select(sysDictItem);
        } else {
            sysDictItemMapper.selectByCondition(ConditionUtil.buildCondition(SysDictItem.class, whereParams));
        }
        if (param.getIncludeIds() != null && !param.getIncludeIds().isEmpty()) {
            param.getIncludeIds().removeIf(id -> {
                return page.getResult().stream().filter(item -> {
                    //return item.getId().equals(id);
                    return false;
                }).count() > 0;
            });
            if (!param.getIncludeIds().isEmpty()) {
                Condition condition = new Condition(SysDictItem.class);
                condition.createCriteria().andIn("id", param.getIncludeIds());
                page.getResult().addAll(0, sysDictItemMapper.selectByCondition(condition));
            }
        }
        return CommonPage.toPage(page);
    }
}
